Imports System.Runtime.Serialization
Imports System.ComponentModel
Imports ATS.Business.Entities.EntityStateClass
Imports System.Collections.ObjectModel

<DataContract()> _
<Serializable()> _
Public Class UserTable
    Implements INotifyPropertyChanged


    Private _isDirty As Boolean

#Region "Primitive Properties"

    <DataMember()>
    Public Overridable Property Unique_Key As Integer

    Private _Username As String
    <DataMember()>
    Public Property Username() As String
        Get
            Return _Username
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _Username Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Username")
                End If
            Else
                Throw New Exception("Please provide value for Username.")
            End If
            _Username = value
            OnPropertyChanged("Username")
        End Set
    End Property


    Private _Hashed As String
    <DataMember()>
    Public Property Hashed() As String
        Get
            Return _Hashed
        End Get

        Set(ByVal value As String)
            If Not value Is Nothing Then
                If value <> _Hashed Then
                    Me.DataStateChanged( _
                    EntityStateEnum.Modified, _
                    "Hashed")
                End If
            Else
                Throw New Exception("Please provide value for Password.")
            End If
            _Hashed = value
            OnPropertyChanged("Hashed")
        End Set
    End Property
    <DataMember()>
    Public Overridable Property Salt As String

    Private _LastPasswordChangeDateTime As Nullable(Of Date)
    <DataMember()>
    Public Property LastPasswordChangeDateTime As Nullable(Of Date)
        Get
            Return _LastPasswordChangeDateTime
        End Get

        Set(ByVal value As Nullable(Of Date))
            If value Is Nothing Then
                Dim x As String = ""
            End If
            _LastPasswordChangeDateTime = value
        End Set
    End Property
    Private _AccountLocked As Nullable(Of Boolean)
    <DataMember()>
    Public Property AccountLocked As Nullable(Of Boolean)
        Get
            Return _AccountLocked
        End Get

        Set(ByVal value As Nullable(Of Boolean))

            If value <> _AccountLocked Then
                Me.DataStateChanged( _
                EntityStateEnum.Modified, _
                "AccountLocked")
            End If
            _AccountLocked = value
            OnPropertyChanged("AccountLocked")
        End Set
    End Property

    Private _LockedDateTime As Nullable(Of Date)
    <DataMember()>
    Public Overridable Property LockedDateTime As Nullable(Of Date)
        Get
            Return _LockedDateTime
        End Get

        Set(ByVal value As Nullable(Of Date))
            If value Is Nothing Then
                Dim x As String = ""
            End If
            _LockedDateTime = value
        End Set
    End Property

    <DataMember()>
    Public Overridable Property NumberOfPasswordTry As Nullable(Of Integer)

    Private _LastLogin As Nullable(Of Date)
    <DataMember()>
    Public Overridable Property LastLogin As Nullable(Of Date)
        Get
            Return _LastLogin
        End Get

        Set(ByVal value As Nullable(Of Date))
            If value Is Nothing Then
                Dim x As String = ""
            End If
            _LastLogin = value
        End Set
    End Property

    <DataMember()>
    Public Overridable Property ChangePassword As Nullable(Of Boolean)
    Public ReadOnly Property IsDirty() As Boolean
        Get
            Return Me.EntityState <> EntityStateEnum.Unchanged
        End Get
    End Property
#End Region



#Region "Support Procedures"
    Public Sub ResetDirtyBit()
        Me.EntityState = EntityStateEnum.Unchanged
    End Sub

    Public Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private _EntityState As EntityStateEnum
    Public Property EntityState() As EntityStateEnum
        Get
            Return _EntityState
        End Get
        Private Set(ByVal value As EntityStateEnum)
            _EntityState = value
        End Set
    End Property

    Private Sub DataStateChanged(ByVal dataState As  _
    EntityStateEnum, ByVal propertyName As String)
        ' Raise the event
        If Not String.IsNullOrEmpty(propertyName) Then
            RaiseEvent PropertyChanged(Me, _
            New PropertyChangedEventArgs(propertyName))
        End If

        ' If the state is deleted, mark it as deleted
        If dataState = EntityStateEnum.Deleted Then
            Me.EntityState = dataState
        End If

        If Me.EntityState = _
                       EntityStateEnum.Unchanged Then
            Me.EntityState = dataState
        End If
    End Sub

    Public Sub New()
    End Sub

    Public Sub New(Unique_Key As Integer, _
                   Username As String, _
                   Hashed As String, _
                   Salt As String, _
                   LastPasswordChangeDateTime As Nullable(Of Date), _
                   AccountLocked As Nullable(Of Boolean), _
                   LockedDateTime As Nullable(Of Date), _
                   NumberOfPasswordTry As Nullable(Of Integer), _
                   LastLogin As Nullable(Of Date), _
                   ChangePassword As Nullable(Of Boolean))
        _Unique_Key = Unique_Key
        _Username = Username
        _Hashed = Hashed
        _Salt = Salt
        _LastPasswordChangeDateTime = LastPasswordChangeDateTime
        _AccountLocked = AccountLocked
        _LockedDateTime = LockedDateTime
        _NumberOfPasswordTry = NumberOfPasswordTry
        _LastLogin = LastLogin
        _ChangePassword = ChangePassword

    End Sub
#End Region


End Class

<Serializable()> _
Public Enum AuthenticationResult
    Authorised = 0
    InvalidUsernameOrPassword = 1
    AccountLocked = 2
    AccountDoesNotExist = 3
End Enum


